/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MainFrame.java
 *
 * Created on 22.12.2011, 16:37:35
 */
package gui;

import actionThreads.PaintThread;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import weapons.Weapon;
import playerData.PlayerMove;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import loadData.Resources;
import playerData.Player;
import weapons.WeaponSammlung;

/**
 *
 * @author Chris
 */
public class MainFrame extends javax.swing.JFrame
{

    private Player p1, p2;
    private WeaponSammlung wpS;
    private PlayerMove pm1, pm2;
    private PaintThread pt;
    private String player1name, player2name;
    private Resources res;
    private boolean pause = false, gameRunning = false, gameOpen = false;
    private long cooldownTimeP1, cooldownTimeP2;

    /** Creates new form MainFrame */
    public MainFrame()
    {
        initComponents();
        try
        {
            res = new Resources();
        } catch (Exception ex)
        {
            JOptionPane.showMessageDialog(null, "Es gab eine Fehler beim Laden der Ressourcen");
        }
        this.setLocation(10, 10);

        Image rk = res.getRocketR();
        BufferedImage bi = new BufferedImage(rk.getWidth(null) / 3, rk.getHeight(null) / 3, BufferedImage.TYPE_INT_ARGB);
        Graphics g = bi.createGraphics();
        g.drawImage(rk, 0, 0, bi.getWidth(), bi.getHeight(), null);
        ImageIcon rkI = new ImageIcon(bi);
        lbVorschau1.setIcon(rkI);
        lbVorschau2.setIcon(rkI);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        pnPlayerChoose = new javax.swing.JPanel();
        btWeiter = new javax.swing.JButton();
        pnPlayer1 = new javax.swing.JPanel();
        lbPlayer1 = new javax.swing.JLabel();
        tfPlayer1Name = new javax.swing.JTextField();
        lbPlayer1Name = new javax.swing.JLabel();
        lbWaffen1 = new javax.swing.JLabel();
        cbPlayer1Weapon = new javax.swing.JComboBox();
        lbVorschau1 = new javax.swing.JLabel();
        pnPlayer2 = new javax.swing.JPanel();
        lbPlayer2 = new javax.swing.JLabel();
        lbPlayer2Name = new javax.swing.JLabel();
        tfPlayer2Name = new javax.swing.JTextField();
        lbWaffen2 = new javax.swing.JLabel();
        cbPlayer2Weapon = new javax.swing.JComboBox();
        lbVorschau2 = new javax.swing.JLabel();
        pnGamePane = new javax.swing.JPanel();
        pnSpielflaeche = new javax.swing.JPanel();
        pnEigenschaften = new javax.swing.JPanel();
        pnButtons = new javax.swing.JPanel();
        btSpielStarten = new javax.swing.JButton();
        btPause = new javax.swing.JButton();
        btHauptmenue = new javax.swing.JButton();
        pnStatus = new javax.swing.JPanel();
        lbSpieler = new javax.swing.JLabel();
        lbSpieler2 = new javax.swing.JLabel();
        player1HP = new javax.swing.JProgressBar();
        player2HP = new javax.swing.JProgressBar();
        lbPlayer1HP = new javax.swing.JLabel();
        lbPlayer2HP = new javax.swing.JLabel();
        pnAnleitung = new javax.swing.JPanel();
        btSettingsBack = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tpAnleitung = new javax.swing.JTextPane();
        pnUeber = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tpUeber = new javax.swing.JTextPane();
        btUeberBack = new javax.swing.JButton();
        pnMainMenu = new javax.swing.JPanel();
        btStartGame = new javax.swing.JButton();
        btExit = new javax.swing.JButton();
        lbTitle = new javax.swing.JLabel();
        lbSubTitle = new javax.swing.JLabel();
        btAbout = new javax.swing.JButton();
        btSettings = new javax.swing.JButton();
        lbCopyright = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();

        pnPlayerChoose.setLayout(new java.awt.BorderLayout());

        btWeiter.setText("Weiter");
        btWeiter.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                onPlayerWeiter(evt);
            }
        });
        pnPlayerChoose.add(btWeiter, java.awt.BorderLayout.PAGE_START);

        pnPlayer1.setLayout(new java.awt.GridBagLayout());

        lbPlayer1.setFont(new java.awt.Font("Tahoma", 1, 14));
        lbPlayer1.setText("Player 1:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        pnPlayer1.add(lbPlayer1, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.ipadx = 150;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        pnPlayer1.add(tfPlayer1Name, gridBagConstraints);

        lbPlayer1Name.setText("Name:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        pnPlayer1.add(lbPlayer1Name, gridBagConstraints);

        lbWaffen1.setText("Waffen:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        pnPlayer1.add(lbWaffen1, gridBagConstraints);

        cbPlayer1Weapon.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Rakete", "Kornspitz" }));
        cbPlayer1Weapon.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                onCbWaffeP1Changed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        pnPlayer1.add(cbPlayer1Weapon, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 50;
        gridBagConstraints.ipady = 120;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        pnPlayer1.add(lbVorschau1, gridBagConstraints);

        pnPlayerChoose.add(pnPlayer1, java.awt.BorderLayout.LINE_START);

        pnPlayer2.setLayout(new java.awt.GridBagLayout());

        lbPlayer2.setFont(new java.awt.Font("Tahoma", 1, 14));
        lbPlayer2.setText("Player 2:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        pnPlayer2.add(lbPlayer2, gridBagConstraints);

        lbPlayer2Name.setText("Name:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        pnPlayer2.add(lbPlayer2Name, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.ipadx = 150;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        pnPlayer2.add(tfPlayer2Name, gridBagConstraints);

        lbWaffen2.setText("Waffen:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        pnPlayer2.add(lbWaffen2, gridBagConstraints);

        cbPlayer2Weapon.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Rakete", "Kornspitz" }));
        cbPlayer2Weapon.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                onCbWaffeP2Changed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        pnPlayer2.add(cbPlayer2Weapon, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 50;
        gridBagConstraints.ipady = 120;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        pnPlayer2.add(lbVorschau2, gridBagConstraints);

        pnPlayerChoose.add(pnPlayer2, java.awt.BorderLayout.CENTER);

        pnGamePane.setLayout(new java.awt.BorderLayout());

        pnSpielflaeche.setBackground(new java.awt.Color(255, 255, 255));
        pnSpielflaeche.setForeground(new java.awt.Color(255, 255, 255));

        javax.swing.GroupLayout pnSpielflaecheLayout = new javax.swing.GroupLayout(pnSpielflaeche);
        pnSpielflaeche.setLayout(pnSpielflaecheLayout);
        pnSpielflaecheLayout.setHorizontalGroup(
            pnSpielflaecheLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 500, Short.MAX_VALUE)
        );
        pnSpielflaecheLayout.setVerticalGroup(
            pnSpielflaecheLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 251, Short.MAX_VALUE)
        );

        pnGamePane.add(pnSpielflaeche, java.awt.BorderLayout.CENTER);

        pnEigenschaften.setMaximumSize(new java.awt.Dimension(500, 500));
        pnEigenschaften.setPreferredSize(new java.awt.Dimension(300, 90));
        pnEigenschaften.setLayout(new java.awt.GridLayout(1, 2));

        pnButtons.setBorder(javax.swing.BorderFactory.createTitledBorder("Buttons"));
        pnButtons.setFocusable(false);
        pnButtons.setLayout(new java.awt.GridBagLayout());

        btSpielStarten.setText("Spiel starten");
        btSpielStarten.setFocusable(false);
        btSpielStarten.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                onGameStart(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        pnButtons.add(btSpielStarten, gridBagConstraints);

        btPause.setText("Spiel pausieren");
        btPause.setEnabled(false);
        btPause.setFocusable(false);
        btPause.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                onGamePause(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        pnButtons.add(btPause, gridBagConstraints);

        btHauptmenue.setText("Hauptmenü");
        btHauptmenue.setFocusable(false);
        btHauptmenue.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                onHauptmenu(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridheight = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        pnButtons.add(btHauptmenue, gridBagConstraints);

        pnEigenschaften.add(pnButtons);

        pnStatus.setBorder(javax.swing.BorderFactory.createTitledBorder("Status"));
        pnStatus.setFocusable(false);
        pnStatus.setLayout(new java.awt.GridBagLayout());

        lbSpieler.setText("Player 1");
        lbSpieler.setFocusable(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        pnStatus.add(lbSpieler, gridBagConstraints);

        lbSpieler2.setText("Player 2");
        lbSpieler2.setFocusable(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        pnStatus.add(lbSpieler2, gridBagConstraints);

        player1HP.setForeground(new java.awt.Color(255, 0, 0));
        player1HP.setFocusable(false);
        player1HP.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                onPlayer1HpChanged(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.ipadx = 200;
        pnStatus.add(player1HP, gridBagConstraints);

        player2HP.setForeground(new java.awt.Color(255, 0, 0));
        player2HP.setFocusable(false);
        player2HP.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                onPlayer2HpChanged(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.ipadx = 200;
        pnStatus.add(player2HP, gridBagConstraints);

        lbPlayer1HP.setText("100");
        lbPlayer1HP.setFocusable(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 0);
        pnStatus.add(lbPlayer1HP, gridBagConstraints);

        lbPlayer2HP.setText("100");
        lbPlayer2HP.setFocusable(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 0);
        pnStatus.add(lbPlayer2HP, gridBagConstraints);

        pnEigenschaften.add(pnStatus);

        pnGamePane.add(pnEigenschaften, java.awt.BorderLayout.PAGE_START);

        pnAnleitung.setLayout(new java.awt.GridBagLayout());

        btSettingsBack.setText("Zurück");
        btSettingsBack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                onSettingsBack(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        pnAnleitung.add(btSettingsBack, gridBagConstraints);

        tpAnleitung.setEditable(false);
        tpAnleitung.setText("Anleitung\n\nW, A, S, D um den Spieler 1 zu bewegen\nLeertaste um mit Spieler 1 zu schießen\n\nPfeiltasten um den Spieler 2 zu bewegen\n# um mit Spieler 2 zu schießen");
        jScrollPane1.setViewportView(tpAnleitung);

        pnAnleitung.add(jScrollPane1, new java.awt.GridBagConstraints());

        pnUeber.setLayout(new java.awt.GridBagLayout());

        tpUeber.setText("Diese Spiel befindet sich derzeit noch in der Beta-Phase.\nFür weitere Informationen \nVisit: theweed.square7.ch");
        jScrollPane2.setViewportView(tpUeber);

        pnUeber.add(jScrollPane2, new java.awt.GridBagConstraints());

        btUeberBack.setText("Zurück");
        btUeberBack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                onUeberBack(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        pnUeber.add(btUeberBack, gridBagConstraints);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("StickMan Revenge");
        addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                onKeyDown(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                onKeyUp(evt);
            }
        });

        btStartGame.setForeground(new java.awt.Color(0, 0, 255));
        btStartGame.setText("Spiel Starten");
        btStartGame.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                onStartGame(evt);
            }
        });

        btExit.setForeground(new java.awt.Color(0, 0, 255));
        btExit.setText("Exit");
        btExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                onExit(evt);
            }
        });

        lbTitle.setFont(new java.awt.Font("Tahoma", 1, 24));
        lbTitle.setForeground(new java.awt.Color(102, 102, 255));
        lbTitle.setText("StickMan Revenge");

        lbSubTitle.setForeground(new java.awt.Color(0, 0, 255));
        lbSubTitle.setText("Hauptmenü");

        btAbout.setForeground(new java.awt.Color(0, 0, 255));
        btAbout.setText("Über");
        btAbout.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                onUeberClicked(evt);
            }
        });

        btSettings.setForeground(new java.awt.Color(0, 0, 255));
        btSettings.setText("Anleitung");
        btSettings.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                onSettingsClicked(evt);
            }
        });

        lbCopyright.setText("Copyright by Weed, Zink und Lex");

        jLabel1.setText("Beta v0.9");

        javax.swing.GroupLayout pnMainMenuLayout = new javax.swing.GroupLayout(pnMainMenu);
        pnMainMenu.setLayout(pnMainMenuLayout);
        pnMainMenuLayout.setHorizontalGroup(
            pnMainMenuLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnMainMenuLayout.createSequentialGroup()
                .addGroup(pnMainMenuLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnMainMenuLayout.createSequentialGroup()
                        .addGap(123, 123, 123)
                        .addGroup(pnMainMenuLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(btStartGame, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 143, Short.MAX_VALUE)
                            .addComponent(btSettings, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 143, Short.MAX_VALUE)
                            .addComponent(btAbout, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 143, Short.MAX_VALUE)
                            .addComponent(btExit, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(pnMainMenuLayout.createSequentialGroup()
                        .addGap(84, 84, 84)
                        .addComponent(lbTitle))
                    .addGroup(pnMainMenuLayout.createSequentialGroup()
                        .addGap(166, 166, 166)
                        .addComponent(lbSubTitle)))
                .addContainerGap(97, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnMainMenuLayout.createSequentialGroup()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 193, Short.MAX_VALUE)
                .addComponent(lbCopyright))
        );
        pnMainMenuLayout.setVerticalGroup(
            pnMainMenuLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnMainMenuLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lbTitle)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lbSubTitle)
                .addGap(59, 59, 59)
                .addComponent(btStartGame)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btSettings)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btAbout)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btExit)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 57, Short.MAX_VALUE)
                .addGroup(pnMainMenuLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbCopyright)
                    .addComponent(jLabel1)))
        );

        getContentPane().add(pnMainMenu, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void onExit(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_onExit
        //Exit-Button Code (MainMenu)
        System.exit(0);
    }//GEN-LAST:event_onExit

    private void onStartGame(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_onStartGame
        //Start-Button Code (MainMenu)
        pnMainMenu.setVisible(false);
        this.remove(pnMainMenu);
        this.add(pnPlayerChoose);
        pnPlayerChoose.setVisible(true);
        this.setSize(this.getWidth() + 20, this.getHeight() + 20);
    }//GEN-LAST:event_onStartGame

    private void onGameStart(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_onGameStart
        ///Start-Button Code (Game)
        if (pause)
        {
            resumeAllThreads();
            btSpielStarten.setEnabled(false);
            btSpielStarten.setText("Spiel starten");
            btPause.setEnabled(true);
        } else
        {
            btPause.setEnabled(true);
            startGame();
        }
    }//GEN-LAST:event_onGameStart

    private void startGame()
    {
        //Egal wie hoch der x-Wert ist, die Figur wird nur bei der Hälfte gesetzt
        //y Wert Funktioniert
        //TODO Fix Bug and remove Testcode
        //Testcode:
        int p2x, p2y;
        p2x = (pnSpielflaeche.getWidth() * 2) - 70;
        p2y = pnSpielflaeche.getHeight() - 70;
        //System.out.println(p2x);
        //System.out.println(p2y);

        p1 = new Player(res.getPlayer1Left(), res.getPlayer1Right(), 20, 20, player1name, 1);
        pm1 = new PlayerMove(p1);
        p2 = new Player(res.getPlayer2Left(), res.getPlayer2Right(), p2x, p2y, player2name, 2);
        pm2 = new PlayerMove(p2);
        wpS = new WeaponSammlung();
        pt = new PaintThread(pm1, pm2, wpS, res);
        pt.start();
        this.requestFocus();
        btSpielStarten.setEnabled(false);
        gameRunning = true;
        gameOpen = true;
    }

    private void onHauptmenu(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_onHauptmenu
        //Hauptmenu-Button Code (Game)
        stopAllThreads();
        showMainMenu();
    }//GEN-LAST:event_onHauptmenu

    private void pauseAllThreads()
    {
        pt.pauseThread();
        pm1.pauseThread();
        pm2.pauseThread();
        for (int i = 0; i < wpS.size(); i++)
        {
            wpS.get(i).pauseThread();
        }
        pause = true;
    }

    private void resumeAllThreads()
    {
        pm1.resumeThread();
        pm2.resumeThread();
        for (int i = wpS.size() - 1; i >= 0; i--)
        {
            wpS.get(i).resumeThread();
        }
        pt.resumeThread();
        pause = false;
    }

    private void stopAllThreads()
    {
        pt.stopThread();
        pm1.stopThread();
        pm2.stopThread();
        for (int i = wpS.size() - 1; i >= 0; i--)
        {
            wpS.get(i).stopThread();
            wpS.remove(i);
        }
    }

    private void onKeyDown(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_onKeyDown
        if (!pause)
        {
            int keyCode = evt.getKeyCode();
            switch (keyCode)
            {
                case 37:
                {
                    //Key: Pfeiltaste links
                    pm2.setLeft(true);
                    break;
                }
                case 38:
                {
                    //Key: Pfeiltaste oben
                    pm2.setUp(true);
                    break;
                }
                case 39:
                {
                    //Key: Pfeiltaste rechts
                    pm2.setRight(true);
                    break;
                }
                case 40:
                {
                    //Key: Pfeiltaste unten
                    pm2.setDown(true);
                    break;
                }
                case 65:
                {
                    //Key: A
                    pm1.setLeft(true);
                    break;
                }
                case 87:
                {
                    //Key: W
                    pm1.setUp(true);
                    break;
                }
                case 68:
                {
                    //Key: D
                    pm1.setRight(true);
                    break;
                }
                case 83:
                {
                    //Key: S
                    pm1.setDown(true);
                    break;
                }
                case 32:
                {
                    //Key: Leertaste
                    //Cooldownzeit: 1 sec
                    if ((System.currentTimeMillis() - cooldownTimeP1) < 1000)
                    {
                        break;
                    }
                    Weapon wp = new Weapon(pm1.getDirection(), pm1, pm1, pm2, res.getPlayer1WeaponLeft(), res.getPlayer1WeaponRight());
                    wpS.add(wp);
                    cooldownTimeP1 = System.currentTimeMillis();
                    break;
                }
                case 520:
                {
                    //Key: #
                    //aktuelle Cooldownzeit: 1 sec
                    if ((System.currentTimeMillis() - cooldownTimeP2) < 1000)
                    {
                        break;
                    }
                    Weapon wp = new Weapon(pm2.getDirection(), pm2, pm1, pm2, res.getPlayer2WeaponLeft(), res.getPlayer2WeaponRight());
                    wpS.add(wp);
                    cooldownTimeP2 = System.currentTimeMillis();
                    break;
                }
            }
        }
    }//GEN-LAST:event_onKeyDown

    private void onKeyUp(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_onKeyUp
        if (!pause)
        {
            int keyCode = evt.getKeyCode();
            switch (keyCode)
            {
                case 37:
                {
                    //Key: Pfeiltaste links
                    pm2.setLeft(false);
                    break;
                }
                case 38:
                {
                    //Key: Pfeiltaste oben
                    pm2.setUp(false);
                    break;
                }
                case 39:
                {
                    //Key: Pfeiltaste rechts
                    pm2.setRight(false);
                    break;
                }
                case 40:
                {
                    //Key: Pfeiltaste unten
                    pm2.setDown(false);
                    break;
                }
                case 65:
                {
                    //Key: A
                    pm1.setLeft(false);
                    break;
                }
                case 87:
                {
                    //Key: W
                    pm1.setUp(false);
                    break;
                }
                case 68:
                {
                    //Key: D
                    pm1.setRight(false);
                    break;
                }
                case 83:
                {
                    //Key: S
                    pm1.setDown(false);
                    break;
                }
            }
        }
    }//GEN-LAST:event_onKeyUp

    private void onPlayerWeiter(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_onPlayerWeiter
        player1name = tfPlayer1Name.getText();
        player2name = tfPlayer2Name.getText();
        int zz = 6;
        if ("".equals(player1name))
        {
            zz = randomPlayerName1();
        }
        if ("".equals(player2name))
        {
            randomPlayerName2(zz);
        }
        pnPlayerChoose.setVisible(false);
        //Player Namen werden aus dem Textfeld gelöscht.
        //--> Müssen bei neuem Spiel nicht wieder gelöscht werden
        tfPlayer1Name.setText("");
        tfPlayer2Name.setText("");
        this.remove(pnPlayerChoose);
        this.add(pnGamePane);
        pnGamePane.setVisible(true);
        this.setExtendedState(JFrame.MAXIMIZED_BOTH);
        //HP Bars bekommen Ueberschriften
        lbSpieler.setText(player1name);
        lbSpieler2.setText(player2name);
        player1HP.setValue(100);
        player2HP.setValue(100);
        //TODO read text from combo boxes for images
        String player1WeaponName = (String) cbPlayer1Weapon.getSelectedItem();
        String player2WeaponName = (String) cbPlayer2Weapon.getSelectedItem();
        res.setPlayer1Image("vogi");
        res.setPlayer2Image("vogi");
        res.setWeaponPlayer1(player1WeaponName);
        res.setWeaponPlayer2(player2WeaponName);
    }//GEN-LAST:event_onPlayerWeiter

    private void onGamePause(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_onGamePause
        //Bug found!:
        //Wenn man gleich spiel pausieren klickt ohne es zu Starten kann man nur zwischen
        //Spiel pausieren und Spiel fortsetzten hin und herschalten aber nichts passiert
        //Lösungsvorschlag:
        //Überprüfung ob das Spiel schon gestarted wurde oder nicht
        btSpielStarten.setEnabled(true);
        btSpielStarten.setText("Spiel fortsetzen");
        btPause.setEnabled(false);
        pauseAllThreads();
    }//GEN-LAST:event_onGamePause

private void onCbWaffeP1Changed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_onCbWaffeP1Changed
    if (cbPlayer1Weapon.getSelectedItem().equals("Rakete"))
    {
        Image img = res.getRocketR();
        BufferedImage bi = new BufferedImage(img.getWidth(null) / 3, img.getHeight(null) / 3, BufferedImage.TYPE_INT_ARGB);
        Graphics g = bi.createGraphics();
        g.drawImage(img, 0, 0, bi.getWidth(), bi.getHeight(), null);
        ImageIcon ico = new ImageIcon(bi);
        lbVorschau1.setIcon(ico);
    } else if (cbPlayer1Weapon.getSelectedItem().equals("Kornspitz"))
    {
        Image img = res.getKornspitzR();
        BufferedImage bi = new BufferedImage(img.getWidth(null) / 3, img.getHeight(null) / 3, BufferedImage.TYPE_INT_ARGB);
        Graphics g = bi.createGraphics();
        g.drawImage(img, 0, 0, bi.getWidth(), bi.getHeight(), null);
        ImageIcon ico = new ImageIcon(bi);
        lbVorschau1.setIcon(ico);
    }
}//GEN-LAST:event_onCbWaffeP1Changed

    private void onPlayer1HpChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_onPlayer1HpChanged
        if (player1HP.getValue() == 0)
        {
            lbPlayer1HP.setText("" + player1HP.getValue());
            gameRunning = false;
            repaint();
            endGame(p2);
            return;
        }
        lbPlayer1HP.setText("" + player1HP.getValue());
    }//GEN-LAST:event_onPlayer1HpChanged

    private void onPlayer2HpChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_onPlayer2HpChanged
        if (player2HP.getValue() == 0)
        {
            lbPlayer2HP.setText("" + player2HP.getValue());
            gameRunning = false;
            repaint();
            endGame(p1);
            return;
        }
        lbPlayer2HP.setText("" + player2HP.getValue());
    }//GEN-LAST:event_onPlayer2HpChanged

    private void onCbWaffeP2Changed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_onCbWaffeP2Changed
        if (cbPlayer2Weapon.getSelectedItem().equals("Rakete"))
        {
            Image img = res.getRocketR();
            BufferedImage bi = new BufferedImage(img.getWidth(null) / 3, img.getHeight(null) / 3, BufferedImage.TYPE_INT_ARGB);
            Graphics g = bi.createGraphics();
            g.drawImage(img, 0, 0, bi.getWidth(), bi.getHeight(), null);
            ImageIcon ico = new ImageIcon(bi);
            lbVorschau2.setIcon(ico);
        } else if (cbPlayer2Weapon.getSelectedItem().equals("Kornspitz"))
        {
            Image img = res.getKornspitzR();
            BufferedImage bi = new BufferedImage(img.getWidth(null) / 3, img.getHeight(null) / 3, BufferedImage.TYPE_INT_ARGB);
            Graphics g = bi.createGraphics();
            g.drawImage(img, 0, 0, bi.getWidth(), bi.getHeight(), null);
            ImageIcon ico = new ImageIcon(bi);
            lbVorschau2.setIcon(ico);
        }
    }//GEN-LAST:event_onCbWaffeP2Changed

    private void onSettingsClicked(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_onSettingsClicked
        this.pnMainMenu.setVisible(false);
        this.remove(pnMainMenu);
        this.add(pnAnleitung);
        this.pnAnleitung.setVisible(true);
    }//GEN-LAST:event_onSettingsClicked

    private void onSettingsBack(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_onSettingsBack
        this.pnAnleitung.setVisible(false);
        this.remove(pnAnleitung);
        this.add(pnMainMenu);
        this.pnMainMenu.setVisible(true);
    }//GEN-LAST:event_onSettingsBack

    private void onUeberBack(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_onUeberBack
        this.pnUeber.setVisible(false);
        this.remove(pnUeber);
        this.add(pnMainMenu);
        this.pnMainMenu.setVisible(true);
    }//GEN-LAST:event_onUeberBack

    private void onUeberClicked(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_onUeberClicked
        this.pnMainMenu.setVisible(false);
        this.remove(pnMainMenu);
        this.add(pnUeber);
        this.pnUeber.setVisible(true);
    }//GEN-LAST:event_onUeberClicked

    public void showMainMenu()
    {
        pnGamePane.setVisible(false);
        btSpielStarten.setEnabled(true);
        btSpielStarten.setText("Spiel starten");
        this.remove(pnGamePane);
        this.add(pnMainMenu);
        pnMainMenu.setVisible(true);
        this.setSize(433, 357);
        this.setLocation(10, 10);
        gameOpen = false;
    }

    private int randomPlayerName1()
    {
        String zufall = "";
        int zz = 0;
        Random rand = new Random();
        zz = rand.nextInt(9) + 1;
        switch (zz)
        {
            case 1:
                zufall = "Schweinsbraten";
                break;
            case 2:
                zufall = "Dracula";
                break;
            case 3:
                zufall = "AstroAffenFaust";
                break;
            case 4:
                zufall = "Spongebob";
                break;
            case 5:
                zufall = "R2D2";
                break;
            case 6:
                zufall = "Unwichtiger Nebencharacter";
                break;
            case 7:
                zufall = "ZINK";
                break;
            case 8:
                zufall = "Obi Wan Kenobi";
                break;
            case 9:
                zufall = "Asterix";
                break;
        }
        player1name = zufall;
        return zz;
    }

    private void randomPlayerName2(int zz)
    {
        String zufall2 = "";
        int zz2 = 0;
        Random rand = new Random();
        zz2 = rand.nextInt(9) + 1;
        if (zz2 == zz)
        {
            zz2++;
            if (zz2 > 9)
            {
                zz2 = zz - 1;
            }
        }
        switch (zz2)
        {
            case 1:
                zufall2 = "Schweinsbraten";
                break;
            case 2:
                zufall2 = "Dracula";
                break;
            case 3:
                zufall2 = "AstroAffenFaust";
                break;
            case 4:
                zufall2 = "Spongebob";
                break;
            case 5:
                zufall2 = "R2D2";
                break;
            case 6:
                zufall2 = "Unwichtiger Nebencharacter";
                break;
            case 7:
                zufall2 = "ZINK";
                break;
            case 8:
                zufall2 = "Obi Wan Kenobi";
                break;
            case 9:
                zufall2 = "Asterix";
                break;
        }
        player2name = zufall2;
    }

    public void endGame(Player winner)
    {
        stopAllThreads();
        if (JOptionPane.showConfirmDialog(this, winner.getName() + " hat gewonnen!\nNeues Spiel starten?", winner.getName() + " hat gewonnen!", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
        {
            player1HP.setValue(100);
            player2HP.setValue(100);
            startGame();
        } else
        {
            showMainMenu();
            btPause.setEnabled(false);
        }
    }

    @Override
    public void paint(Graphics g)
    {
        this.paintComponents(g);
        //Falls Spiel pausiert ist dann zeichne pauseScreen neu
        if (pause || (!gameRunning && gameOpen))
        {
            Graphics pnS = pnSpielflaeche.getGraphics();
            pnS.drawImage(pt.getScreen(), 0, 0, pnSpielflaeche.getWidth(), pnSpielflaeche.getHeight(), null);
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[])
    {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try
        {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels())
            {
                if ("Nimbus".equals(info.getName()))
                {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex)
        {
            java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex)
        {
            java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex)
        {
            java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex)
        {
            java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable()
        {

            @Override
            public void run()
            {
                new MainFrame().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btAbout;
    private javax.swing.JButton btExit;
    private javax.swing.JButton btHauptmenue;
    private javax.swing.JButton btPause;
    private javax.swing.JButton btSettings;
    private javax.swing.JButton btSettingsBack;
    private javax.swing.JButton btSpielStarten;
    private javax.swing.JButton btStartGame;
    private javax.swing.JButton btUeberBack;
    private javax.swing.JButton btWeiter;
    private javax.swing.JComboBox cbPlayer1Weapon;
    private javax.swing.JComboBox cbPlayer2Weapon;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel lbCopyright;
    private javax.swing.JLabel lbPlayer1;
    public javax.swing.JLabel lbPlayer1HP;
    private javax.swing.JLabel lbPlayer1Name;
    private javax.swing.JLabel lbPlayer2;
    public javax.swing.JLabel lbPlayer2HP;
    private javax.swing.JLabel lbPlayer2Name;
    public static javax.swing.JLabel lbSpieler;
    private javax.swing.JLabel lbSpieler2;
    private javax.swing.JLabel lbSubTitle;
    private javax.swing.JLabel lbTitle;
    private javax.swing.JLabel lbVorschau1;
    private javax.swing.JLabel lbVorschau2;
    private javax.swing.JLabel lbWaffen1;
    private javax.swing.JLabel lbWaffen2;
    public static javax.swing.JProgressBar player1HP;
    public static javax.swing.JProgressBar player2HP;
    private javax.swing.JPanel pnAnleitung;
    private javax.swing.JPanel pnButtons;
    private javax.swing.JPanel pnEigenschaften;
    private javax.swing.JPanel pnGamePane;
    private javax.swing.JPanel pnMainMenu;
    private javax.swing.JPanel pnPlayer1;
    private javax.swing.JPanel pnPlayer2;
    private javax.swing.JPanel pnPlayerChoose;
    public static javax.swing.JPanel pnSpielflaeche;
    private javax.swing.JPanel pnStatus;
    private javax.swing.JPanel pnUeber;
    private javax.swing.JTextField tfPlayer1Name;
    private javax.swing.JTextField tfPlayer2Name;
    private javax.swing.JTextPane tpAnleitung;
    private javax.swing.JTextPane tpUeber;
    // End of variables declaration//GEN-END:variables
}
